//
//  WishListViewController.m
//  CenturyInterconnection
//
//  Created by Tian Wei You on 16/5/10.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "WishListViewController.h"
#import "WishListTableViewCell.h"
#import "CommodityDetailsTableViewController.h"
#import "otherButton.h"

#define WishListCell  @"WishListTableViewCell"

@interface WishListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *allDatas;

@end

@implementation WishListViewController

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,64, KScreenWidth, KScreenHeight-64) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.allDatas=[NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title=@"WISH LIST";
    
    [self makeNaviButton];
    [self getDatas];
    [self makeView];
    [self.tableView registerNib:[UINib nibWithNibName:WishListCell bundle:nil] forCellReuseIdentifier:WishListCell];
}

-(void)getDatas{
        // http://viprunningonline.com/api/index.php?main_page=products_new
    if ([UserSession instance].isLogin == YES) {
        NSString*urlStrr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
        NSDictionary*paramss=@{@"main_page":@"my_wishlist",@"zenid":[UserSession instance].cookie};
        HttpManager*managerr=[[HttpManager alloc]init];
        [managerr getDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
            
            NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
            
            if ([number isEqualToString:@"1"]) {
                self.allDatas=[data[@"data"] mutableCopy];
                [self makeView];
                [self.tableView reloadData];
            }
            
        }];
    }
    
}

-(void)makeView{
    self.view.backgroundColor=[UIColor whiteColor];
    
    if (self.allDatas.count==0) {
        UILabel *noProductsLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, ACTUAL_HEIGHT(300), KScreenWidth, ACTUAL_HEIGHT(30))];
        //noProductsLabel.backgroundColor=[UIColor yellowColor];
        noProductsLabel.text=@"No products in Wish List";
        noProductsLabel.textColor=RGBACOLOR(153, 153, 153, 1);
        noProductsLabel.font = FONT_SYSTEM_28;
        noProductsLabel.textAlignment=1;
        [self.view addSubview:noProductsLabel];
    }else{
        [self.view addSubview:self.tableView];
    }

}

#pragma mark---------导航栏

-(void)makeNaviButton{
    
    self.view.backgroundColor = [UIColor blackColor];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(23), ACTUAL_HEIGHT(23));
    [leftButton addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem=item;
}

#pragma mark 返回事件
-(void)comeBack{
    
    [[SlideNavigationController sharedInstance].rightMenu viewWillAppear:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark TableView设置

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(iPhone5)
        return ACTUAL_HEIGHT(130);
    return ACTUAL_HEIGHT(155);
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allDatas.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WishListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:WishListCell];
//    cell.delegate=self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.allDatas.count>0) {
        UIImageView *imageView1=[cell viewWithTag:1];
        imageView1.backgroundColor=[UIColor whiteColor];
        [imageView1 sd_setImageWithURL:[NSURL URLWithString:self.allDatas[indexPath.row][@"products_image"]] placeholderImage:nil];
        imageView1.clipsToBounds = YES;
        imageView1.contentMode = UIViewContentModeScaleAspectFit;
        
        UILabel *label2=[cell viewWithTag:2];
        label2.text=self.allDatas[indexPath.row][@"products_name"];
        
        UIButton *button3=[cell viewWithTag:3];
        button3.tag=indexPath.row+10000;
        [button3 addTarget:self action:@selector(comeBackToProductsDetails:) forControlEvents:UIControlEventTouchUpInside];
        
        otherButton *button4=[cell viewWithTag:4];
        button4.tagg=indexPath.row;
        [button4 addTarget:self action:@selector(deleteTheLine:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return cell;
}

#pragma mark 每个Cell里“view”的点击事件

-(void)comeBackToProductsDetails:(UIButton*)bt1{
    
    CommodityDetailsTableViewController *vc=[[CommodityDetailsTableViewController alloc]init];
    vc.idStr=self.allDatas[bt1.tag-10000][@"products_id"];
    vc.imgUrlStr = self.allDatas[bt1.tag-10000][@"products_image"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 每个Cell里“delete”的点击事件

-(void)deleteTheLine:(otherButton*)bt2{
    // http://viprunningonline.com/api/index.php? main_page=wishlist_actions
    NSString*urlStr=[NSString stringWithFormat:@"%@main_page=wishlist_actions",HTTP_ADDRESS];
    
    NSDictionary*params=@{@"act":@"remove",
                          @"products_id":self.allDatas[bt2.tagg][@"products_id"],
                          @"zenid":[UserSession instance].cookie};
    HttpManager*manager=[[HttpManager alloc]init];
    
    [manager postDataFromNetworkWithUrl:urlStr parameters:params compliation:^(id data, NSError *error) {
        
        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
        
        if ([number isEqualToString:@"1"]) {
            [JRToast showWithText:@"Delete the success！"];
            [self.allDatas removeObject:self.allDatas[bt2.tagg]];
            [self.tableView reloadData];
            
        }else{
            [JRToast showWithText:data[@"status_message"]];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
